﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.iss.inventory;
using com.iss.inventory.Entity;
using com.iss.inventory.CustomModels;

namespace com.iss.inventory.Controllers
{
    public class StationeryRequestController
    {
        public RequestListShowModel rlsm;
        private InvManagementEntities invManEn;
        public StationeryRequestController()
        {
            invManEn = new InvManagementEntities();
           
        }
        public int InsertStationeryRequest(RequestListModel rlm)
        {
            RequestList rl = new RequestList();
            rl.EmployeeID = rlm.EmployeeID;
            rl.ApprovedbyID = rlm.ApproveID;
            //rl.DateSubmitted = rlm.DateReceieve;
            rl.DateSubmitted = DateTime.Now;
            rl.DateReceived = null;
            rl.SpecialRequest = rlm.SpecialRequest;
            rl.Status = rlm.Status;
            rl.Remarks = null;
            invManEn.RequestLists.AddObject(rl);
            invManEn.SaveChanges();

            var lastrequestID = (from s in invManEn.RequestLists orderby s.RequestID descending select s).FirstOrDefault<RequestList>();
            return lastrequestID.RequestID;
        }
        public int ReterieveLastOutput()
        {
             return (from s in invManEn.RequestLists orderby s.RequestID descending select s.RequestID).First();
        }

        public void AddRequestListDetails(int requestListID,string itemID,int qty)
        {
      //       String name = (from s in invManEn.StationeryItems where s.ItemID == 110006  select s.Description).First();
            int temp_itemID = int.Parse(itemID);
            //int itemId= (from s in invManEn.StationeryItems where s.Description.Contains(description) select s.ItemID).First();
            RequestDetail requestDetail = new RequestDetail();
            requestDetail.RequestID = requestListID;
            requestDetail.ItemID = temp_itemID;
            requestDetail.QtyRequested = qty;
            requestDetail.QtyDelivered = 0;
            invManEn.RequestDetails.AddObject(requestDetail);
            invManEn.SaveChanges();

        }

        public List<RequestListShowModel> GetAllRequestList(int approveID)
        {
         
           
            List<RequestListShowModel> return_Value=new List<RequestListShowModel>();
            List<RequestList> temp_store= (from a in invManEn.RequestLists where a.ApprovedbyID==approveID select a).ToList<RequestList>();
            foreach (RequestList rl in temp_store)
            {
                    rlsm = new RequestListShowModel(); 
                rlsm.EmployeeName = (from e in invManEn.Employees where e.EmployeeID == rl.EmployeeID select e.Name).FirstOrDefault();
                rlsm.RequestitemID = rl.RequestID;
                rlsm.RequestDate = Convert.ToDateTime(rl.DateSubmitted);
                rlsm.Status = rl.Status;
                return_Value.Add(rlsm);
            }
            return return_Value;
        }

        public List<object> ShowRequestData(int requestId)
        {
            var list = (from c in invManEn.RequestDetails where c.RequestID == requestId select new { ItemID = c.ItemID, Description = c.StationeryItem.Description, QtyLeft = c.QtyRequested, UnitOfMeasure = c.StationeryItem.UnitofMeasure });
            return list.ToList<object>();
        }

        public void DeleteRequestData(int requestID, int stationeryID)
        {
            RequestDetail rd = (from i in invManEn.RequestDetails where i.RequestID == requestID && i.ItemID == stationeryID select i).First();
            invManEn.DeleteObject(rd);

        }
    }
}
